<template>
	<view class="orderdetails">
		
		<view class="order_content">
			<view class="order_top" v-if="Item.state=='待付款'">
				<image src="/pagesB/static/Orderdet_fukuan@2x.png" mode=""></image>
				<view class="top_content">
					<view class="content_title">等待买家付款</view>
					<view class="content_text">{{ time }}后自动取消订单</view>
				</view>
			</view>
			<view class="order_top" v-if="Item.state=='待发货'">
				<image src="/pagesB/static/Orderdet_fukuan@2x.png" mode=""></image>
				<!-- <image src="../../../../static/Login_code@2x.png" mode=""></image> -->
				<view class="top_content">
					<view class="content_title">等待卖家发货</view>
				</view>
			</view>
			<view class="order_top" v-if="Item.state=='待收货'">
				<image src="/pagesB/static/Orderdet_fukuan@2x1.png" mode=""></image>
				<view class="top_content">
					<view class="content_title">卖家已发货</view>
					<view class="content_text">{{ time }}后自动确认收货</view>
				</view>
			</view>
			<view class="order_top" v-if="Item.state=='待评价'">
				<image src="/pagesB/static/pingjia.png" mode=""></image>
				<view class="top_content">
					<view class="content_title">等待买家评价</view>
				</view>
			</view>
			<view class="order_top" v-if="Item.state=='已完成'">
				<image src="/pagesB/static/wancheng.png" mode=""></image>
				<view class="top_content">
					<view class="content_title">订单已完成</view>
				</view>
			</view>
			<view class="order_top" v-if="Item.state=='已关闭'">
				<image src="/pagesB/static/guanbi.png" mode=""></image>
				<view class="top_content">
					<view class="content_title">订单已关闭</view>
				</view>
			</view>
			<view class="order_express" v-if="express.length" @click="$linkTo('/pagesB/express?id='+Item.order_sn)">
				<view class="itemLeft">
					<view class="addressItem">
						<view class="ItemAddress">{{ express[0].context}}</view>
						<view class="ItemTitle">
							<view class="ItemPhone">{{ express[0].time }}</view>
						</view>
					</view>
				</view>
				<view class="itemRight"></view>
			</view>
			
			<view class="order_address">
				<view class="itemLeft">
					<view class="addressItem">
						<view class="ItemTitle">
							<view class="ItemName">{{ Item.address_name }}</view>
							<view class="ItemPhone">{{ Item.address_phone }}</view>
						</view>
						<view class="ItemAddress">{{ Item.province + Item.city + Item.district + Item.address }}</view>
					</view>
				</view>
			</view>
			<view class="order_amount">
				<view class="item_content" v-for="(i, index) in Item.goods" :key="index">
					<view class="Item_content">
					<image :src="i.image||'/pagesB/static/微信图片_20201124091755.png'" mode=""></image>
					<view class="ItemContent">
						<view class="title">{{ i.name }}</view>
						<view class="parameter">
							<view class="parameter1">{{ i.option_name }}</view>
						</view>
						<view class="price_ctrlbox">
							<view class="price">￥{{ i.price }}</view>
							<view class="ctrlbox">x{{ i.quantity }}</view>
						</view>
					</view>
					</view>
					<view class="item_buttom" v-if="Item.state=='待发货'">
						<view class="buttom" @click="torefund(i.goods_id,'待发货')">
							申请退款
						</view>
					</view>
					<!-- <view class="item_buttom" v-if="Item.state=='待收货'">
						<view class="buttom" @click="torefund(i.goods_id,'待收货')">
							申请售后
						</view>
					</view> -->
				</view>
				<view class="order_price1">
					<view class="order_price">
						<view class="price_left">商品总价</view>
						<view class="price_right">￥{{ Item.goods_amount }}</view>
					</view>
					<view class="order_price">
						<view class="price_left">运费</view>
						<view class="price_right">￥{{ Item.express_amount }}</view>
					</view>
					<view class="order_price">
						<view class="price_left">优惠</view>
						<view class="price_right">￥{{ Item.coupon_amount }}</view>
					</view>
					<view class="order_price">
						<view class="price_left">实付款</view>
						<view class="price_right1">￥{{ Item.order_amount }}</view>
					</view>
				</view>
			</view>
			<view class="order_comment">买家留言:{{ Item.comment==''?'暂无留言':Item.comment }}</view>
			<view class="order_id">
				<view class="order_copy">
					<view class="order_sn">订单号：{{ Item.order_sn }}</view>
					<view class="order_buttom" @click="copy(Item.order_sn)">复制</view>
				</view>
				<view class="order_date">订单创建时间：{{ Item.date_add }}</view>
				<view class="order_date" v-if="Item.pay_name" >支付方式：{{ Item.pay_name }}</view>
			</view>
		</view>
		<view class="order_bottom savebottom" v-if="Item.state!='待发货'">
			<view class="order_payment" @click="buttom(Item.state=='待评价'?'去评价':Item.state=='待付款'?'立即付款':'确认收货',Item)" v-if="Item.state!='已完成'&&Item.state!='已关闭'">{{Item.state=='待评价'?'去评价':Item.state=='待付款'?'立即付款':'确认收货'}}</view>
			<view class="order_cancel" @click="close(Item.state=='待付款'?'取消订单':'删除订单',Item)">{{Item.state=='待付款'?'取消订单':'删除订单'}}</view>
		</view>
	</view>
</template>

<script>
import md5 from '@/common/api/md5.js';
	import {requestUrl} from '@/common/config.js'
export default {
	data() {
		return {
			order_sn: '',
			access_token: '',
			time: '',
			Item: [],
			express:[]
		};
	},
	onShow() {
		this.get()
	},
	methods:{
		get(){
			this.order_sn = JSON.parse(uni.getStorageSync('order_sn'));
			let time = Math.round(new Date().getTime() / 1000).toString();
			this.access_token = JSON.parse(uni.getStorageSync('access_token'));
			let _this = this;
			let Time = ['time', time].join('=');
			let content = ['content=pinet.FeiMao.2017'].join();
			let arr = [content, Time].sort(function(a, b) {
				a - b;
			});
			let newarr = md5(arr.join('&'));
			uni.request({
				url: requestUrl+'/app/order/getOrderDetail',
				method: 'GET',
				data: {
					access_token: this.access_token,
					time: time,
					sign: newarr,
					order_sn: this.order_sn
				},
				success: function(res) {
					console.log(JSON.stringify(res));
					if (res.data.errcode == 0) {
						function dateTO(data) {
							//data为时间戳
							var date = new Date(data);
							var Y = date.getFullYear() + '-';
							var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
							var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
							var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
							var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
							var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
							var strDate = Y + M + D + h + m + s; //转换为年月日时分秒
							// var strDate = Y + M + D; //转换为年月日
							return strDate;
						}
						function dateT1(data) {
							//data为时间戳
							var date = new Date(data);
							var Y = date.getFullYear() + '-';
							var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
							var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + '天';
							var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + '时';
							var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + '分';
							var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
							var strDate = D + h + m; //转换为年月日时分秒
							// var strDate = Y + M + D; //转换为年月日
							return strDate;
						}
			
						_this.time = dateT1(res.data.content.order.date_end - res.data.content.order.date_add * 1000);
						res.data.content.order.date_add = dateTO(res.data.content.order.date_add * 1000);
						res.data.content.order.date_end = dateTO(res.data.content.order.date_end * 1000);
						res.data.content.order.date_pay = dateTO(res.data.content.order.date_pay * 1000);
						res.data.content.order.date_send = dateTO(res.data.content.order.date_send * 1000);
						if(res.data.content.order.date_received){
							res.data.content.order.date_received = dateTO(res.data.content.order.date_received * 1000);
						}
						_this.Item = res.data.content.order;
						console.log(_this.time);
						if(res.data.content.express){
							_this.express=res.data.content.express
						}
						console.log(res.data.content,_this.express);
					}
				}
			});
		},
		copy(e){
			uni.setClipboardData({
			    data: e,
			    success: function () {
			        console.log('success');
			    }
			});
		},
		torefund(e,i){
			this.Item.goods.forEach(v=>{
				if(e==v.goods_id){
					uni.setStorage({
						key: 'goods',
						data: JSON.stringify(v),
						success: function () {
						}
					});
				}
			})
			if(i=='待发货'){
				uni.navigateTo({
					url:'/pagesB/orderdetails/refund'
				})
			}else if(i=='待收货'){
				uni.navigateTo({
					url:'/pagesB/orderdetails/afterSale'
				})
			}
		},
		buttom(e,i){
			console.log(e,i)
			if(e=='立即付款'){
				this.$emit('update',i)
				uni.navigateTo({
					url:'/pagesA/PaymentMethod/PaymentMethod',
					  success: function(res) {
					    // 通过eventChannel向被打开页面传送数据
					    res.eventChannel.emit('acceptDataFromOpenerPage', i)
					  }
				})
			}else if(e=='确认收货'){
				uni.showModal({
					title:'提示',
					content:'是否确定收货',
						 success: (res)=> {
						        if (res.confirm) {
						            this.$request('/app/order/delivery','post',{
						            	access_token:uni.getStorageSync('access_token').replace(/^\"|\"$/g,''),
						            	order_sn:i.order_sn
						            }).then(res=>{
						            	uni.showToast({
						            		title:res.message
						            	})
						            	this.get()
						            })
						        } else if (res.cancel) {
						            console.log('用户点击取消');
						        }
						    }
						
				})
				
			}else if(e=='去评价'){
				uni.navigateTo({
					url:'/pagesB/orderdetails/evaluate/evaluate',
					  success: function(res) {
					    // 通过eventChannel向被打开页面传送数据
					    res.eventChannel.emit('evaluate', i)
					  }
				})
				
			}
		},
		close(e,i){
			console.log(e,i)
			if(e=='取消订单'){
				this.$request('/app/order/cancel','post',{
					access_token:uni.getStorageSync('access_token').replace(/^\"|\"$/g,''),
					order_sn:i.order_sn
				}).then(res=>{
					uni.showToast({
						title:res.message
					})
					uni.navigateBack({
						delta:1
					})
				})
			}else if(e=='删除订单'){
				this.$request('/app/order/deleteOrder','post',{
					access_token:uni.getStorageSync('access_token').replace(/^\"|\"$/g,''),
					order_sn:i.order_sn
				}).then(res=>{
					uni.showToast({
						title:res.message
					})
					uni.navigateBack({
						delta:1
					})
				})
			}
		}
	}
};
</script>

<style lang="less" scoped>
.orderdetails {
	height: 100%;
	background-color: #f3f4f5;
	display: flex;
	flex-direction: column;
	.order_top {
		padding: 40rpx 70rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		background: #db1b22;
		image {
			width: 120rpx;
			height: 120rpx;
		}
		.top_content {
			flex: 1;
			margin-left: 50rpx;
			.content_title {
				font-size: 30rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #ffffff;
				margin-bottom: 10rpx;
			}
			.content_text {
				font-size: 24rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #ffffff;
			}
		}
	}
	
	.order_content{
		flex: 1;
		overflow-y: scroll;
		.order_express {
			padding: 20rpx 30rpx;
			background: url(/pagesB/static/icon_logistics_00.png) no-repeat 20rpx 78rpx;
			background-size: 28rpx 24rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			background-color: #fff;
			position: relative;
			border-bottom: 1px solid #eeeeee;
			.itemRight{
				width: 20px;
				height: 20px;
				background: url(/pagesB/static/direction_right_gray.png) no-repeat 0px 0px;
				background-size: 100% 100%;
				position: absolute;
				bottom: 70rpx;
				right: 30rpx;
			}
			.itemLeft {
				margin-left: 30rpx;
				.addressnone {
					padding: 40rpx 0;
					font-size: 26rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #9da5a8;
				}
				.addressContent {
					flex: 1;
					background-color: #f3f4f5;
					padding-top: 20rpx;
				}
				.addressItem {
					padding: 30rpx;
					background: #fff;
					.Active {
						font-size: 24rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #ffffff;
						background: #db1b22;
						border-radius: 6rpx;
						margin-right: 20rpx;
						padding: 6rpx 14rpx;
					}
					.ItemTitle {
						display: flex;
						align-items: center;
						height: 34rpx;
						.ItemName {
							font-size: 32rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #111111;
						}
						.ItemPhone {
							font-size: 26rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #111111;
						}
					}
					.ItemAddress {
						font-size: 26rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #9da5a8;
						margin-bottom: 16rpx;
					}
				}
			}
		}
		.order_address {
			padding: 20rpx 30rpx;
			margin: 0 0 10rpx;
			background: url(/pagesB/static/order_adress.png) no-repeat 20rpx 78rpx;
			background-size: 16px;
			display: flex;
			align-items: center;
			justify-content: space-between;
			background-color: #fff;
			.itemLeft {
				margin-left: 30rpx;
				.addressnone {
					padding: 40rpx 0;
					font-size: 26rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #9da5a8;
				}
				.addressContent {
					flex: 1;
					background-color: #f3f4f5;
					padding-top: 20rpx;
				}
				.addressItem {
					padding: 30rpx;
					background: #fff;
					.Active {
						font-size: 24rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #ffffff;
						background: #db1b22;
						border-radius: 6rpx;
						margin-right: 20rpx;
						padding: 6rpx 14rpx;
					}
					.ItemTitle {
						display: flex;
						margin-bottom: 16rpx;
						align-items: center;
						height: 34rpx;
						.ItemName {
							font-size: 32rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #111111;
						}
						.ItemPhone {
							margin-left: 22rpx;
							font-size: 26rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #111111;
						}
					}
					.ItemAddress {
						font-size: 26rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #9da5a8;
					}
				}
			}
		}
		.order_amount {
			background-color: #fff;
			margin-bottom: 10rpx;
			.item_content {
				padding: 20rpx;
				border-bottom: 1px solid #eeeeee;
				.Item_content{
				display: flex;
					
				}
				.item_buttom{
					display: flex;
					justify-content: flex-end;
					padding-top: 20rpx;
					.buttom{
						padding: 10rpx 20rpx;
						font-size: 24rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #111111;
						background: #FFFFFF;
						border-radius: 50px;
						border: 1px solid #777777;
					}
				}
				image {
					height: 180rpx;
					width: 180rpx;
					margin-right: 16rpx;
				}
				.ItemContent {
					flex: 1;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					.title {
						height: 72rpx;
						font-size: 28rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #111111;
					}
					.parameter {
						height: 34rpx;
						font-size: 24rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #9da5a8;
					}
					.price_ctrlbox {
						display: flex;
						justify-content: space-between;
					}
					.price {
						font-size: 28rpx;
						font-family: PingFangSC-Medium, PingFang SC;
						font-weight: 500;
						color: #111111;
					}
					.ctrlbox {
						font-size: 24rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #9da5a8;
					}
				}
			}
			.order_price1 {
				padding: 0 24rpx 24rpx 24rpx;
			}
			.order_price {
				margin-top: 24rpx;
				height: 100%;
				display: flex;
				justify-content: space-between;
				.price_left {
					font-size: 24rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #111111;
				}
				.price_right {
					font-size: 24rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #111111;
				}
				.price_right1 {
					font-size: 28rpx;
					font-family: PingFangSC-Medium, PingFang SC;
					font-weight: 500;
					color: #db1b22;
				}
			}
		}
		.order_comment {
			padding: 32rpx 22rpx;
			margin-bottom: 10rpx;
			background-color: #fff;
			font-size: 24rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #111111;
		}
		.order_id {
			padding: 24rpx;
			margin-bottom: 10rpx;
			background-color: #fff;
			.order_copy {
				display: flex;
				.order_sn {
					font-size: 24rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #111111;
				}
				.order_buttom {
					width: 60rpx;
					height: 30rpx;
					border-radius: 16rpx;
					border: 1px solid #777777;
					font-size: 20rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #111111;
					text-align: center;
					margin-left: 20rpx;
				}
			}
			.order_date {
				font-size: 24rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #111111;
				margin-top: 10rpx;
			}
		}
		
	}
	.order_bottom{
		display: flex;
		padding: 14rpx 20rpx;
		flex-direction: row-reverse;
		background-color: #fff;
		border-top: 1px solid #EBECEC;
		.order_cancel{
			font-size: 28rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #333333;
			width: 230rpx;
			height: 70rpx;
			line-height: 70rpx;
			background: #FFFFFF;
			border-radius: 36rpx;
			text-align: center;
			border: 1px solid #999999;
		}
		.order_payment{
			font-size: 28rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
			width: 230rpx;
			height: 70rpx;
			line-height: 70rpx;
			text-align: center;
			background: #DB1B22;
			border-radius: 36rpx;
			margin-left: 20rpx;
		}
	}
	.savebottom {
		padding-bottom: 0;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
	}
}
</style>
